package org.xserver.component.redis;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Some basic operation on redis
 * 
 * @author idoldefine
 * @since 2014/10/04
 * 
 */
public interface RedisOperations {
	/**
	 * Store key and value pair in redis
	 * 
	 * @param key
	 * @param value
	 */
	public void setKeyValue(String key, String value);
	
	/**
	 * Store key and value pair in redis with sync
	 * 
	 * @param key
	 * @param value
	 * 
	 * add by mjjhuang
	 */
	public List<Object> setKeyValueSync(String key, String value ,Integer timeout);

	/**
	 * Store key and value with expire seconds
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 */
	public void setKeyValueExpire(String key, String value, int seconds);

	/**
	 * Append to value , like: {key:1, value:value1}, after
	 * append("1","value2"), {key:1, value:value1value2} new: key:1
	 * 
	 * @param key
	 * @param value
	 */
	public void append(String key, String value);

	/**
	 * Get value by key
	 * 
	 * @param key
	 * @return
	 */
	public String getValue(String key);

	/**
	 * Get value by byte[] key
	 * 
	 * @param key
	 * @return
	 */
	public byte[] getValue(byte[] key);
	
	/**
	 * Set key expire
	 * 
	 * @param key
	 *            the key
	 * @param seconds
	 *            how long the key will survive
	 */
	public void expire(String key, int seconds);

	/**
	 * Delete record by key
	 */
	public void delete(String key);

	/**
	 * Delete records by keys
	 */
	public void delete(String... key);

	/**
	 * Whether the key exist
	 * 
	 * @param key
	 * @return if the key exist return true, else false
	 */
	public boolean exists(String key);

	/**
	 * Push value to list head
	 * 
	 * @param key
	 * @param value
	 */
	public void listLeftPush(String key, String value);

	/**
	 * Push value to list tail
	 * 
	 * @param key
	 * @param value
	 */
	public void listRightPush(String key, String value);

	/**
	 * Pop value from list head
	 * 
	 * @param key
	 * @return
	 */
	public String listLeftPop(String key);

	/**
	 * Pop value from list tail
	 * 
	 * @param key
	 * @return
	 */
	public String listRightPop(String key);

	/**
	 * Return the length of list
	 * 
	 * @param key
	 * @return
	 */
	public long listLength(String key);

	/**
	 * Set value with key at index position
	 * 
	 * @param key
	 * @param index
	 * @param value
	 */
	public void listSet(String key, long index, String value);

	/**
	 * List all values
	 * 
	 * @param key
	 * @return
	 */
	public List<String> listAll(String key);

	/**
	 * List the range values
	 * 
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	public List<String> listRange(String key, long start, long end);

	/**
	 * Set value to hash
	 * 
	 * @param key
	 * @param hash
	 */
	public void hashSet(String key, Map<String, String> hash);

	/**
	 * Set value to hash
	 * 
	 * @param key
	 * @param field
	 * @param value
	 */
	public void hashSet(String key, String field, String value);

	/**
	 * Get value by key
	 * 
	 * @param key
	 * @return
	 */
	public Map<String, String> hashGet(String key);

	/**
	 * Get value by key and field
	 * 
	 * @param key
	 * @param field
	 * @return
	 */
	public String hashGet(String key, String field);

	/**
	 * Get match keys from redis
	 * 
	 * @param pattern
	 *            match pattern
	 * @return set of keys
	 */
	public Set<String> keys(String pattern);

	/**
	 * Delete all the keys of the currently selected DB. This command never
	 * fails.
	 */
	public void flushDB();

	/**
	 * Return the number of keys in the currently selected database.
	 * 
	 * @return
	 */
	public Long dbSize();
}
